import { create } from 'zustand';

const useAIChatStore = create((set) => ({
  messages: [],
  isLoading: false,
  history: [],
  showHistory: false,

  // 添加消息
  addMessage: (message) => {
    set((state) => ({
      messages: [...state.messages, message],
      isLoading: false
    }));
  },

  // 设置加载状态
  setLoading: (loading) => {
    set({ isLoading: loading });
  },

  // 保存对话历史
  saveHistory: (conversation) => {
    set((state) => {
      const newHistory = [...state.history, conversation].slice(-10); // 最多保留10条
      return { history: newHistory };
    });
  },

  // 切换历史记录显示
  toggleHistory: () => {
    set((state) => ({
      showHistory: !state.showHistory
    }));
  },

  // 清空消息
  clearMessages: () => {
    set({ messages: [] });
  }
}));

export default useAIChatStore;